package com.streamer.app.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.bean.PrizeBean

class PrizeAdapter(
    private val prizes: MutableList<PrizeBean> = mutableListOf()
) : RecyclerView.Adapter<PrizeAdapter.PrizeVH>() {

    class PrizeVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvPrizeIndex: TextView = itemView.findViewById(R.id.tv_prize_index)
        val tvPrizeName: TextView = itemView.findViewById(R.id.tv_prize_name)
        val tvPrizeQuantity: TextView = itemView.findViewById(R.id.tv_prize_quantity)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PrizeVH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_prize, parent, false)
        return PrizeVH(view)
    }

    override fun onBindViewHolder(holder: PrizeVH, position: Int) {
        val prize = prizes[position]
        Log.d("PrizeAdapter", "Binding item at position $position: $prize")
        
        holder.tvPrizeIndex.text = (position + 1).toString()
        holder.tvPrizeName.text = prize.name
        holder.tvPrizeQuantity.text = prize.quantity.toString()
        
        Log.d("PrizeAdapter", "Item bound: index=${holder.tvPrizeIndex.text}, name=${holder.tvPrizeName.text}, quantity=${holder.tvPrizeQuantity.text}")
    }

    override fun getItemCount(): Int = prizes.size

    fun updatePrizes(newPrizes: List<PrizeBean>) {
        Log.d("PrizeAdapter", "updatePrizes called with ${newPrizes.size} items")
        Log.d("PrizeAdapter", "newPrizes content: $newPrizes")
        
        prizes.clear()
        prizes.addAll(newPrizes)
        Log.d("PrizeAdapter", "prizes list size after update: ${prizes.size}")
        
        notifyDataSetChanged()
        Log.d("PrizeAdapter", "notifyDataSetChanged called")
    }

    fun addPrize(prize: PrizeBean) {
        prizes.add(prize)
        notifyItemInserted(prizes.size - 1)
    }

    fun clearPrizes() {
        prizes.clear()
        notifyDataSetChanged()
    }
} 